<?php

namespace App\Customer\Controller;

use App\Customer\Model\CaseModel;
use App\Customer\Model\CaseResumeModel;
use EchoPHP\Http\Request;
use EchoPHP\Http\Response\JsonResponse;
use Exception;

class CaseResume extends Controller {

    /**
     * b端人才管理数据
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function resumeList(Request $request) {
        $caseId = $request->getParam('caseid/d', 0);
        $subId = $request->getParam('subid/d', 0);
        $flowatStatus = $request->getParam('flowat');
        $pageNum = $request->getParam('pagenum/d', 1);
        $pageLimit = $request->getParam('pageLimit/d', 10);
        $keycode = $request->getParam('keycodes');

        //校验subid和caseid是否属于该用户
        $hrUid = $this->getEhireHruid();
        $subIds = $this->getSubIds();
        $caseModel = new CaseModel();
        $checkResult = $caseModel->checkSubIdByHrUid($subIds, $subId);

        if ($caseId <= 0 || $subId <= 0 || !$checkResult ) {
            return $this->json([
                'status' => false,
                'message' => '参数出錯！'
            ]);
        }
        $searchParams = [
            'subid' => $subId,
            'caseid' => $caseId,
            'status' => '01'
        ];
        //判断是一级还是2级流程
        $flowatFlowatStatus = '';
        $flowat = '';
        if(strlen($flowatStatus) == 2){
            $flowat = $flowatStatus;
        }elseif (strlen($flowatStatus) == 4) {
            $flowat = substr($flowatStatus, 0, 2);
            $flowatFlowatStatus = $flowatStatus;
        }
        if(!empty($flowat)) {
            $searchParams['flowat'] = $flowat;
        }
        if(!empty($flowatFlowatStatus)) {
            $searchParams['flowat_flowatstatus'] = $flowatFlowatStatus;
        }
        if(!empty($keycode)) {
            $searchParams['keycodes'] = $keycode;
        }




        $caseResumeModel = new CaseResumeModel();
        $resultBody = $caseResumeModel->getCaseResumeInfo($searchParams, $pageNum, $pageLimit);
        return $this->json($resultBody);
    }

    /**
     * Notes:简历对应职位的流程变更记录
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function caseTagList(Request $request) {
        $subId = $request->getParam('subid/d', 0);
        $keyCode = $request->getParam('keycode');
        $subIds = $this->getSubIds();
        $caseModel = new CaseModel();
        $checkResult = $caseModel->checkSubIdByHrUid($subIds, $subId);
        if ($subId <= 0 || !$checkResult || empty($keyCode)) {
            return $this->json([
                'status' => false,
                'message' => '参数出錯！'
            ]);
        }

        $caseResumeTag = (new \App\Web\Model\CaseResume())->getResumeTagHis($subId, $keyCode);
        foreach ($caseResumeTag as &$item) {
            $item['createdate'] = isset($item['createdate']) && !empty($item['createdate']) ? date("Y-m-d H:i", strtotime($item['createdate'])) : '';
            $item['timecontrol'] = isset($item['timecontrol']) && !empty($item['timecontrol']) ? date("Y-m-d H:i", strtotime($item['timecontrol'])) : '';
        }
        return $this->json(
            [
                'status' => true,
                'message' => '获取成功',
                'caseTagList' => $caseResumeTag,
            ]
        );
    }
}